﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.UI.WebControls;
using AutoMapper;
using TutorCenter.DataAccess.Entity;
using TutorCenter.Service.Services;
using TutorCenter.WebApplication.Models;

namespace TutorCenter.WebApplication.Controllers
{
    public class ParentController : Controller
    {
        private readonly IParentServices parentServices;
        private readonly ISubjectServices _subjectServices;
        private readonly IGradeServices gradeServices;

        public ParentController(IParentServices parentServices, ISubjectServices _subjectServices, IGradeServices gradeServices)
        {
            this.parentServices = parentServices;
            this._subjectServices = _subjectServices;
            this.gradeServices = gradeServices;
        }
        // GET: Parent
        public ActionResult Index()
        {
            var listParent = this.parentServices.GetAll().ToList();
            return View("Index",Mapper.Map<List<Parent>, List<ParentView>>(listParent));
        }

        // GET: Parent/Details/5
        public ActionResult Details(int id)
        {
            return View();
        }

        // GET: Parent/Create
        //[Authorize(Roles = "Admin")]
        public ActionResult Add()
        {
            var parent = new ParentAdd
            {
                ListGrade =
                    gradeServices.GetAll()
                        .Select(grade => new SelectListItem {Text = grade.Name, Value = grade.Id.ToString()})
                        .ToList(),
                ListSubject =
                    _subjectServices.GetAll()
                        .Select(subject => new SelectListItem {Text = subject.Name, Value = subject.Id.ToString()})
                        .ToList()
            };
            return View(parent);
        }

        // POST: Parent/Create
        [HttpPost]
        public ActionResult Add(ParentAdd parentAdd)
        {
            if (ModelState.IsValid)
            {
                var parent = Mapper.Map<ParentAdd, Parent>(parentAdd);
                this.parentServices.Add(parent);
            }
            return RedirectToAction("Index");
        }

        // GET: Parent/Edit/5
        public ActionResult Edit(int id)
        {
            return View();
        }

        // POST: Parent/Edit/5
        [HttpPost]
        public ActionResult Edit(int id, FormCollection collection)
        {
            try
            {
                // TODO: Add update logic here

                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

        // GET: Parent/Delete/5
        public ActionResult Delete(int id)
        {
            return View();
        }

        // POST: Parent/Delete/5
        [HttpPost]
        public ActionResult Delete(int id, FormCollection collection)
        {
            try
            {
                // TODO: Add delete logic here

                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }
    }
}
